package com.ruanda.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruanda.domain.Prod;
import com.ruanda.domain.Sku;
import com.ruanda.model.ChangeStock;
import com.ruanda.model.Result;
import com.ruanda.service.ProdService;
import com.ruanda.service.SkuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "商品接口管理")
@RequestMapping("prod/prod")
@RestController
public class ProdController {

    @Autowired
    private ProdService prodService;
    @Autowired
    private SkuService skuService;

    @Operation(summary = "多条件分页查询商品列表")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('prod:prod:page')")
    public Result<Page<Prod>> loadProdPage(@RequestParam Long current,
                                           @RequestParam Long size,
                                           @RequestParam(required = false) String prodName,
                                           @RequestParam(required = false) Integer status) {
        Page<Prod> page = new Page<>(current,size);
        page = prodService.page(page,new LambdaQueryWrapper<Prod>()
                .eq(ObjectUtil.isNotNull(status),Prod::getStatus,status)
                .like(StringUtils.hasText(prodName),Prod::getProdName,prodName)
                .orderByDesc(Prod::getCreateTime)
        );
        return Result.success(page);
    }

    @Operation(summary = "新增商品")
    @PostMapping
    @PreAuthorize("hasAuthority('prod:prod:save')")
    public Result<String> saveProd(@RequestBody Prod prod) {
        Integer count = prodService.saveProd(prod);
        return Result.handle(count>0);
    }

    @Operation(summary = "根据标识查询商品详情")
    @GetMapping("info/{prodId}")
    @PreAuthorize("hasAuthority('prod:prod:info')")
    public Result<Prod> loadProdDetail(@PathVariable Long prodId) {
        Prod prodDetailVo = prodService.queryProdDetailById(prodId);
        return Result.success(prodDetailVo);
    }

    @Operation(summary = "修改商品信息")
    @PutMapping
    @PreAuthorize("hasAuthority('prod:prod:update')")
    public Result<String> modifyProd(@RequestBody Prod prod) {
        Integer count = prodService.modifyProd(prod);
        return Result.handle(count>0);
    }

    @Operation(summary = "批量删除商品")
    @DeleteMapping("{prodIds}")
    @PreAuthorize("hasAuthority('prod:prod:delete')")
    public Result<String> removeProds(@PathVariable List<Long> prodIds) {
        Integer count = prodService.removeProdsByIds(prodIds);
        return Result.handle(count==prodIds.size());
    }

    // feign
    @GetMapping("getProdListByIds")
    public Result<List<Prod>> getProdListByIds(@RequestParam List<Long> ids){
        List<Prod> prods = prodService.listByIds(ids);
        return Result.success(prods);
    }

    @GetMapping("getProdListByCategoryIds")
    public Result<List<Prod>> getProdListByCategoryIds(@RequestParam List<Long> categoryIds){
        List<Prod> prods = prodService.list(new LambdaQueryWrapper<Prod>()
                .in(Prod::getCategoryId,categoryIds)
        );
        return Result.success(prods);
    }
    @GetMapping("getSkuListBySkuIds")
    public Result<List<Sku>> getSkuListBySkuIds(@RequestParam List<Long> skuIds){
        List<Sku> skus = skuService.listByIds(skuIds);
        return Result.success(skus);
    }

    @PostMapping("changeProdAndSkuStock")
    public Result<Boolean> changeProdAndSkuStock(@RequestBody ChangeStock changeStock){
        Boolean changed = prodService.changeProdAndSkuChangeStock(changeStock);
        return Result.success(changed);
    }

    ///////////// 小程序接口 //////////
    @Operation(summary = "小程序根据商品标识查询商品详情")
    @GetMapping("prod/prodInfo")
    public Result<Prod> LoadWxProdInfo(@RequestParam Long prodId) {
        //根据商品标识查询商品详情
        Prod prod = prodService.queryWxProdInfoByProdId(prodId);
        return Result.success(prod);
    }
    
}